package com.qian.service.ddj.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.qian.api.ddj.model.StatusDict;
import com.qian.api.ddj.model.Withdraw;
import com.qian.api.ddj.service.IWithdrawService;
import com.qian.api.ddj.util.HttpRequestUtil;
import com.qian.api.ddj.util.MD5Utils;
import com.qian.api.ddj.util.SignUtils;
import com.qian.service.ddj.dao.BusinessRepository;
import com.qian.service.ddj.dao.WithdrawRepository;
import com.qian.service.ddj.entity.DdjBusiness;
import com.qian.service.ddj.entity.DdjChannel;
import com.qian.service.ddj.entity.DdjWithdraw;
import com.yaowk.api.common.BaseException;
import com.yaowk.api.utils.HttpUtils;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
public class WithdrawServiceImpl extends BaseServiceImpl<Withdraw, DdjWithdraw, Long> implements IWithdrawService {

    @Value("${host:}")
    private String HOST;

    @Autowired
    private WithdrawRepository dao;
    @Autowired
    private BusinessRepository businessDao;


    /**
     * 提现分发器
     *
     * @param withdraw
     */
    @Override
    public void withdraw(Withdraw withdraw) {
        DdjBusiness ddjBusiness = businessDao.findOne(withdraw.getBusinessId());
        DdjChannel ddjChannel = ddjBusiness.getBusinessGroup().getChannel();
        withdraw.setOrderNo(String.valueOf(System.currentTimeMillis()));
        switch (ddjChannel.getCode()) {
            case "xjkj":
                xjkjWithdraw(withdraw, ddjBusiness);
                break;
            case "xjsm":
                xjsmWithdraw(withdraw, ddjBusiness);
                break;
            case "xjwg":
                xjwgWithdraw(withdraw, ddjBusiness);
                break;
//            case "ghtsm":
//                ghtsm(withdraw, ddjBusiness);
//                break;
            case "wjxjksm":
                wjxjksmWithdraw(withdraw, ddjBusiness);
                break;
//            case "easyPay":
//                easyPayWithDraw(withdraw, ddjBusiness);
//                break;
            case "ryf":
                ryfWithDraw(withdraw, ddjBusiness);
                break;
            case "pay01":
                pay01WithDraw(withdraw, ddjBusiness);
                break;
            case "ns":
                nsWithDraw(withdraw, ddjBusiness);
                break;
            case "pay03":
                pay03WithDraw(withdraw, ddjBusiness);
                break;
            case "YLSM":
                ylsmWithDraw(withdraw,ddjBusiness);
                break;
            default:
                throw new BaseException("无提现接口可用");
        }
        simpleSave(withdraw);
    }

    private void ylsmWithDraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        System.out.println("代付。。。。。。。。。。。。。。");
        String gateWay = "http://120.77.22.14:8380/pay/gateway/withdraw.do";//请求
        SortedMap<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20003");//交易码
        requestMap.put("PRT_CODE", ddjBusiness.getBackup1());//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0
        requestMap.put("MERC_ID", ddjBusiness.getAccount());//商户号
        requestMap.put("OUT_WITHDRAW_NO", withdraw.getOrderNo());//订单号  确保接入系统唯一
        requestMap.put("WITHDRAW_MONEY", withdraw.getAmount());//代付金额，单位为元
        requestMap.put("PERSON_NM", withdraw.getCardPerson());//收款人姓名
        requestMap.put("CARD_NO", withdraw.getCardNo());//收款人卡号
        requestMap.put("BNK_NM", withdraw.getCardBank());//收款人银行卡开户行
        requestMap.put("BNK_NO", withdraw.getBackup1());//银行代号 例CMB
        requestMap.put("BNK_CD", withdraw.getBackup2());//联行号
        requestMap.put("CRP_ID_NO", withdraw.getBackup3());//收款人身份证号
        requestMap.put("PHONE_NO", withdraw.getCardPhone());//收款人手机号
        requestMap.put("PAY_ID", "D0");//代付类型  D0额度代付T1普通代付

        String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);//随机字符串
        requestMap.put("TM_SMP", System.currentTimeMillis()+"");//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式
        String sign = MD5Utils.getSign(requestMap, ddjBusiness.getSecretKey());
        System.out.println("数据签名成功 sign=" + sign);
        requestMap.put("SIGN_DAT", sign);//签名数据

        String json = JSON.toJSONString(requestMap);
        String ret = "";
        try {
            ret = HttpRequestUtil.httpPostSendReceive(gateWay, json);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        JSONObject jsonobj = JSON.parseObject(ret);
        if (!"0000".equals(jsonobj.getString("RETURNCODE"))){
            throw new BaseException(jsonobj.getString("RETURNCON"));
        }
    }

    private void pay03WithDraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20003");//交易码
        requestMap.put("PRT_CODE", ddjBusiness.getAccount());//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0

        requestMap.put("MERC_ID", ddjBusiness.getBackup1());//商户号
        requestMap.put("OUT_WITHDRAW_NO", withdraw.getOrderNo());//订单号  确保接入系统唯一
        requestMap.put("WITHDRAW_MONEY", withdraw.getAmount());//代付金额，单位为元

        requestMap.put("PERSON_NM", withdraw.getCardPerson());//收款人姓名
        requestMap.put("CARD_NO", withdraw.getCardNo());//收款人卡号
        requestMap.put("BNK_NM", withdraw.getCardBank());//收款人银行卡开户行
        requestMap.put("BNK_NO", withdraw.getBackup1());//银行代号 例CMB
        requestMap.put("BNK_CD", withdraw.getBackup2());//联行号
        requestMap.put("CRP_ID_NO", withdraw.getBackup3());//收款人身份证号
        requestMap.put("PHONE_NO", withdraw.getCardPhone());//收款人手机号
        requestMap.put("PAY_ID", "D0");//代付类型  D0额度代付T1普通代付

        String nonce_str = System.currentTimeMillis() + "";// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);//随机字符串
        requestMap.put("TM_SMP", nonce_str);//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式
        String sign = DigestUtils.md5Hex(SignUtils.params(requestMap) + "&KEY=" + ddjBusiness.getSecretKey());
        requestMap.put("SIGN_DAT", sign);//签名数据

        String json = JSON.toJSONString(requestMap);
        String receive = HttpUtils.post("http://120.77.22.14:8380/pay/gateway/withdraw.do", json);
        log.info(receive);
        JSONObject data = JSON.parseObject(receive);
        if (!"0000".equalsIgnoreCase(data.getString("RETURNCODE"))) {
            throw new BaseException(data.getString("RETURNCON"));
        }
    }

    private void nsWithDraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("cardNo", withdraw.getCardNo());
        map.put("accName", withdraw.getCardPerson());
        map.put("amount", withdraw.getAmount());
        map.put("notifyUrl", HOST + "/ddj/withDrawNotify/nsNotify");
        map.put("sign", SignUtils.sign(map, ddjBusiness.getSecretKey()));
        map.put("orderCode", "ns_withdraw");
        map.put("pay_number", withdraw.getOrderNo());
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        JSONObject result = JSON.parseObject(sr);
        if (!"000000".equalsIgnoreCase(result.getString("respCode"))) {
            throw new BaseException(result.getString("respInfo"));
        }
    }

    private void pay01WithDraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("merchNo", ddjBusiness.getAccount());
        map.put("settleNo", withdraw.getOrderNo());
        map.put("settleAmount", new BigDecimal(withdraw.getAmount()).multiply(new BigDecimal(100)).setScale(0).toString());
        map.put("accIdentity", withdraw.getBackup1());
        map.put("accType", withdraw.getBackup2());
        map.put("accName", withdraw.getCardPerson());
        map.put("accNo", withdraw.getCardNo());
        map.put("accPhone", withdraw.getCardPhone());
        map.put("unionCode", withdraw.getBackup3());
        map.put("bankCode", withdraw.getBackup4());
        map.put("notifyUrl", HOST + "/ddj/withDrawNotify/pay01Notify");
        map.put("sign", DigestUtils.md5Hex(SignUtils.params(map) + ddjBusiness.getSecretKey()));
        String body = JSON.toJSONString(map);
        log.info(body);
        String receive = HttpUtils.post("http://47.94.208.216:8080/app/doWithdrawSettle.do", body);
        log.info(receive);
        JSONObject result = JSON.parseObject(receive);
        if (!"00000".equalsIgnoreCase(result.getString("respCode"))) {
            throw new BaseException(result.getString("respDesc"));
        }
    }

    private void ryfWithDraw(Withdraw withdraw, DdjBusiness ddjBusiness) {

        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("amount", withdraw.getAmount());
        map.put("goodsName", "代付");
        map.put("bankName", withdraw.getCardBank());
        map.put("mobile", withdraw.getCardPhone());
        map.put("accountno", withdraw.getCardNo());
        map.put("accountName", withdraw.getCardPerson());
        map.put("bankType",withdraw.getBackup1());
        map.put("bankno", withdraw.getBackup2());
        /**参与验签的字段*/
        String sign = SignUtils.sign(map,ddjBusiness.getSecretKey());
//        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "ryf_withdraw");
        map.put("sign", sign);
        map.put("pay_number", withdraw.getOrderNo());
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"00".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
    }

    /**
     * 余额查询分发器
     *
     * @param id
     * @return
     */
    @Override
    public String queryBusinessBalance(Integer id) {
        DdjBusiness ddjBusiness = businessDao.findOne(id);
        DdjChannel ddjChannel = ddjBusiness.getBusinessGroup().getChannel();
        String result;
        switch (ddjChannel.getCode()) {
            case "xjkj":
                result = xjkjQuery(ddjBusiness);
                break;
            case "xjsm":
                result = xjsmQuery(ddjBusiness);
                break;
            case "xjwg":
                result = xjwgQuery(ddjBusiness);
                break;
            case "wjxjksm":
                result = wjxjksmQuery(ddjBusiness);
                break;
//            case "easyPay":
//                result = easyPayQuery(ddjBusiness);
//                break;
            case "ryf":
                result = ryfQuery(ddjBusiness);
                break;
            case "pay01":
                result = pay01Query(ddjBusiness);
                break;
            case "ns":
                result = nsQuery(ddjBusiness);
                break;
            case "pay03":
                result = pay03Query(ddjBusiness);
                break;
            case "YLSM":
                result = ylsmQuery(ddjBusiness);
                break;
            default:
                result = "无查询余额接口";
        }
        return result;
    }

    private String ylsmQuery(DdjBusiness ddjBusiness) {
        String result;
        System.out.println("账户余额查询。。。。。。。。。。。。。。");
        String gateWay = "http://120.77.22.14:8380/pay/gateway/query.do";
        SortedMap<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20005");
        requestMap.put("PRT_CODE", ddjBusiness.getBackup1());
        requestMap.put("VER_NO", "1.0");
        requestMap.put("MERC_ID", ddjBusiness.getAccount());
        String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);
        requestMap.put("TM_SMP", System.currentTimeMillis() + "");
        requestMap.put("SIGN_TYP", "MD5");
        String sign = MD5Utils.getSign(requestMap, ddjBusiness.getSecretKey());
        System.out.println("数据签名成功 sign=" + sign);
        requestMap.put("SIGN_DAT", sign);

        String json = JSON.toJSONString(requestMap);
        String ret = "";
        try {
            ret = HttpRequestUtil.httpPostSendReceive(gateWay, json);
        } catch (IOException e) {
            e.printStackTrace();
            result = "查询余额接口请求异常";
            return result;
        }

        JSONObject jsonobj = JSON.parseObject(ret);
        String status = (String) jsonobj.get("RETURNCODE");
        String message = (String) jsonobj.get("RETURNCON");

        if ("0000".equalsIgnoreCase(jsonobj.getString("RETURNCODE"))) {
            result = jsonobj.getString("D_AMT");
        } else {
            result = jsonobj.getString("RETURNCON");
        }
        return result;


    }

    private String pay03Query(DdjBusiness ddjBusiness) {
        Map<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20005");//交易码
        requestMap.put("PRT_CODE", ddjBusiness.getAccount());//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0

        requestMap.put("MERC_ID", ddjBusiness.getBackup1());//进件成功后返回的商户号
        String time = System.currentTimeMillis() + "";
        requestMap.put("NON_STR", time);//随机字符串
        requestMap.put("TM_SMP", time);//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式

        requestMap.put("SIGN_DAT", DigestUtils.md5Hex(SignUtils.params(requestMap) + "&KEY=" + ddjBusiness.getSecretKey()));
        String body = JSON.toJSONString(requestMap);
        log.info(body);
        String receive = HttpUtils.post("http://120.77.22.14:8380/pay/gateway/unify.do", body);
        log.info(receive);
        JSONObject data = JSON.parseObject(receive);
        String result;
        if ("0000".equalsIgnoreCase(data.getString("RETURNCODE"))) {
            result = data.getString("D_AMT");
        } else {
            result = data.getString("RETURNCON");
        }

        return result;
    }

    private String nsQuery(DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap();
        map.put("userid", ddjBusiness.getAccount());
        map.put("sign", SignUtils.sign(map, ddjBusiness.getSecretKey()));
        map.put("orderCode", "ns_balanceCheck");
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        JSONObject data = JSON.parseObject(sr);
        String result;
        if ("00".equalsIgnoreCase(data.getString("respCode"))) {
            result = data.getString("total_balance");
        } else {
            result = data.getString("respInfo");
        }
        return result;
    }

    private String pay01Query(DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("merchNo", ddjBusiness.getAccount());
        map.put("settleType", "D0");
        map.put("sign", DigestUtils.md5Hex(SignUtils.params(map) + ddjBusiness.getSecretKey()));
        String body = JSON.toJSONString(map);
        String receive = HttpUtils.post("http://47.94.208.216:8080/app/accSingleMax.do", body);
        JSONObject data = JSON.parseObject(receive);
        String result;
        if ("1".equalsIgnoreCase(data.getString("accStatus"))) {
            result = new BigDecimal(data.getString("accAmount")).divide(new BigDecimal(100)).toString();
        } else if ("0".equalsIgnoreCase(data.getString("accStatus"))) {
            result = "冻结";
        } else {
            result = data.getString("respDesc");
        }
        return result;
    }

    private String ryfQuery(DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "ryf_balanceCheck");
        map.put("sign", sign);
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if ("00".equals(result.get("respCode"))) {
            if (ddjBusiness.getAccount().equals(result.get("userid"))) {
                return result.get("total_balance");
            }
        }
        return result.get("respInfo");
    }

    @Override
    public Withdraw findByOrderNo(String orderNo) {
        Withdraw withdraw = new Withdraw();
        withdraw.setOrderNo(orderNo);
        return findOne(withdraw);
    }

    @Override
    public void success(Long id) {
        DdjWithdraw ddjWithdraw = dao.findOne(id);
        ddjWithdraw.setStatus(StatusDict.SUCCESS);
        dao.save(ddjWithdraw);
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected DdjWithdraw model2Entity(Withdraw model) {
        DdjWithdraw ddjWithdraw = null;
        if (isNotEmpty(model)) {
            ddjWithdraw = new DdjWithdraw();
            ddjWithdraw.setId(model.getId());
            ddjWithdraw.setAmount(model.getAmount());
            ddjWithdraw.setBackup1(model.getBackup1());
            ddjWithdraw.setBackup2(model.getBackup2());
            ddjWithdraw.setBackup3(model.getBackup3());
            ddjWithdraw.setBackup4(model.getBackup4());
            ddjWithdraw.setBackup5(model.getBackup5());
            ddjWithdraw.setCardBank(model.getCardBank());
            DdjBusiness ddjBusiness = new DdjBusiness();
            ddjBusiness.setId(model.getBusinessId());
            ddjWithdraw.setBusiness(ddjBusiness);
            ddjWithdraw.setCardNo(model.getCardNo());
            ddjWithdraw.setCardPerson(model.getCardPerson());
            ddjWithdraw.setCardPhone(model.getCardPhone());
            ddjWithdraw.setOrderNo(model.getOrderNo());
            ddjWithdraw.setResult(model.getResult());
            ddjWithdraw.setStatus(model.getStatus());
        }
        return ddjWithdraw;
    }

    @Override
    protected Withdraw entity2model(DdjWithdraw entity) {
        return null;
    }


    private void wjxjksmWithdraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("account_no", withdraw.getCardNo());
        map.put("account_name", withdraw.getCardPerson());
        map.put("amount", withdraw.getAmount());
        map.put("mobile", withdraw.getCardPhone());
        map.put("bank_code", withdraw.getBackup1()); // 银行代码
        map.put("bank_settle_no", withdraw.getBackup2()); // 行别号
        map.put("account_type", withdraw.getBackup3()); // 银行卡类别 1-储蓄卡 2-信用卡
        if (!StringUtils.isEmpty(withdraw.getBackup4()))
            map.put("account_id_card", withdraw.getBackup4());
        /**参与验签的字段*/
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "jxjk_withdraw");
        map.put("sign", sign);
        map.put("pay_number", withdraw.getOrderNo());
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"SUCCESS".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
    }

    private void xjkjWithdraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("accountno", withdraw.getCardNo());
        map.put("accountName", withdraw.getCardPerson());
        map.put("amount", withdraw.getAmount());
        map.put("city", withdraw.getBackup1());
        /**参与验签的字段*/
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_quick_withdraw");
        map.put("sign", sign);
        map.put("pay_number", withdraw.getOrderNo());
        map.put("notify_url", HOST + "/withdraw/xjkjNotify");
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"000000".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
    }

    private void xjsmWithdraw(Withdraw withdraw, DdjBusiness ddjBusiness){
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("accountno", withdraw.getCardNo());
        map.put("accountName", withdraw.getCardPerson());
        map.put("amount", withdraw.getAmount());
        map.put("city", withdraw.getBackup1());
        /**参与验签的字段*/
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_codeWithdraw");
        map.put("sign", sign);
        map.put("pay_number", withdraw.getOrderNo());
        map.put("notify_url", HOST + "/withdraw/xjsmNotify");
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"000000".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
    }

    private void xjwgWithdraw(Withdraw withdraw, DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        map.put("accountno", withdraw.getCardNo());
        map.put("accountName", withdraw.getCardPerson());
        map.put("amount", withdraw.getAmount());
        map.put("city", withdraw.getBackup1());
        /**参与验签的字段*/
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_withdraw");
        map.put("sign", sign);
        map.put("pay_number", withdraw.getOrderNo());
        map.put("notify_url", HOST + "/withdraw/xjwgNotify");
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if (!"000000".equals(result.get("respCode"))) {
            throw new BaseException(result.get("respInfo"));
        }
    }

    private String xjkjQuery(DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_userQuery");
        map.put("sign", sign);
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if ("000000".equals(result.get("respCode"))) {
            if (ddjBusiness.getAccount().equals(result.get("userid"))) {
                return result.get("quick_balance");
            }
        }
        return result.get("respInfo");
    }

    private String xjwgQuery(DdjBusiness ddjBusiness){
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_userQuery");
        map.put("sign", sign);
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if ("000000".equals(result.get("respCode"))) {
            if (ddjBusiness.getAccount().equals(result.get("userid"))) {
                return result.get("gateway_balance");
            }
        }
        return result.get("respInfo");
    }
    private String xjsmQuery(DdjBusiness ddjBusiness){
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        String sign = SignUtils.params(map);
        sign = DigestUtils.md5Hex(sign + ddjBusiness.getSecretKey());
        map.put("orderCode", "xj_userQuery");
        map.put("sign", sign);
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if ("000000".equals(result.get("respCode"))) {
            if (ddjBusiness.getAccount().equals(result.get("userid"))) {
                return result.get("code_balance");
            }
        }
        return result.get("respInfo");
    }

    private String wjxjksmQuery(DdjBusiness ddjBusiness) {
        Map<String, String> map = new TreeMap<>();
        map.put("userid", ddjBusiness.getAccount());
        String sign = DigestUtils.md5Hex(SignUtils.params(map) + ddjBusiness.getSecretKey());
        map.put("sign", sign);
        map.put("orderCode", "jxjk_balanceQuery");
        String baowen = HttpUtils.formatData(map);
        log.info("上送的报文为：" + baowen);
        String sr = HttpUtils.post("http://extman.kefupay.cn/pay/pay_mobile.action", baowen);
        log.info(sr);
        Map<String, String> result = JSON.parseObject(sr, Map.class);
        if ("SUCCESS".equals(result.get("respCode"))) {
            if (ddjBusiness.getAccount().equals(result.get("userid"))) {
                return result.get("total_balance");
            }
        }
        return result.get("respInfo");
    }

//    public void ghtsm(Withdraw withdraw, DdjBusiness ddjBusiness) {
//        Map<String,String> map = new TreeMap<>();
//        String key="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAI2I+RLEdoRY3MB8xJXTBYRAev3bwDZR1LBrWn3o6oRkdJfBui6a2f8tKq+azgx/rJU3KChuaGT8bn3J28KnjV6HCpg2ra1s2FGpnAmkNca6z0bcsNTkr0f2kZ1x3D72sP8lVRoYKjEjSrTdi+2ieDL/2YQDzPBTmilnd58Xs2OZAgMBAAECgYEAg6RO5mHNSjPMv0xNHH11uBixTMTD9sIN7TkspcepuRImT7Bkl0x4TpuYBFUcKQvnXbI7q79lYehYbK7/tAFbvpAQ/lrLrwyPlvdiPWNyy0pSl8Z7yuLSDLfqLzH8wO6utAbAEmHwJEQwiniDn2K2Jk57QlBunAOMP2vi36ev8bECQQDxOfzWth8t9MN5t4J/6uHKVEOXxbagxGTU0CkFolIPtSXJMHRFD+o8vJ2W88C/ObS0bsQPF6soVtpyzmV8FGPnAkEAljP/GDoJ0W6rgXJgHE/wdXQZfWG2o1lGgapxoia6io6k6EevivUGORoZdIfqEXfxEA8+CKC5H5py/S9vJwEMfwJAe4uEZ/8clu/d0Y/gAV2tyG9BsxCqyUnsjmYSLHWG686ODK2JTvmosPS1DWPH2uhWGXcFaOlpWp6pANWeel8DDwJADZdC28RUoxiqeBoLTESHc3k/3TJ8PieT06PCiHOMqYDitwnVfnNRTtVnvD2YFmCN9XALoug4F/33raUfH55erQJAAepJZt1FhxDzVWiNdO0RrBSXwshzPbOs3v+Bf0IPUFnCrQaw14pjKevH3ughdZ2D73wxpbmqQLKsom+0qIX/dQ==";
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
//        map.put("service","payForAnotherOne");
//        map.put("merchantNo",ddjBusiness.getAccount());
//        map.put("version","V2.0");
//        map.put("orderNo",withdraw.getOrderNo());
//        map.put("accountProp",withdraw.getBackup1());//1私人2公司
//        map.put("accountNo",withdraw.getCardNo());
//        map.put("accountName",withdraw.getCardPerson());
//        map.put("bankGenneralName",withdraw.getCardBank());
//        map.put("bankName",withdraw.getBackup4());//支行名字
//        map.put("bankCode",withdraw.getBackup5());//开户行行号
//        map.put("bankProvcince",withdraw.getBackup2());//省
//        map.put("bankCity",withdraw.getBackup3());//市
//        map.put("orderAmount",withdraw.getAmount());
//        map.put("orderTime",sdf.format(date));
//        String sign=SignUtils.getsign(map,ddjBusiness.getSecretKey());
//        map.put("sign",sign);
//        String url="http://service.gaohuitong.com/PayApi/agentPay";
//        String result = null;
//        try {
//            result = HttpClientUtil.post(url, map);
//        } catch (Exception e) {
//            e.printStackTrace();
//            throw new BaseException();
//        }
//        Map<String,String> mapResult = JSON.parseObject(result,Map.class);
//        if (!"10000".equals(mapResult.get("dealCode"))){
//            throw new BaseException(mapResult.get("dealMsg"));
//        }
//
//    }
//    public String easyPayQuery(DdjBusiness ddjBusiness){
//        ApiClient apiClient = ApiClientFactory.newClient("https://gw.ak47pay.com/native/", "ZFCK24000JAXS", "2017102821494327000");
//        ApiRequestBuilder builder = ApiRequestBuilder.newInstance("com.opentech.cloud.easypay.balance.query", "0.0.1");
//        builder.addParameter("merchantNo", "ZFCK24000JAXS");
//        ApiResponse response = apiClient.invoke(builder.build());
//        Map<String,String> map = response.getData(Map.class);
//        log.info(map.get("balance"));
//        return map.get("balance");
//    }
//    public void easyPayWithDraw(Withdraw withdraw, DdjBusiness ddjBusiness){
//        ApiClient apiClient = ApiClientFactory.newClient("https://gw.ak47pay.com/native/", "ZFCK24000JAXS", "2017102821494327000");
//        ApiRequestBuilder builder = ApiRequestBuilder.newInstance("com.opentech.cloud.easypay.balance.pay", "0.0.1");
//        builder.addParameter("merchantNo", "ZFCK24000JAXS");
//        builder.addParameter("outPayNo",withdraw.getOrderNo());
//        builder.addParameter("payPassword",withdraw.getBackup1());//支付密码
//        builder.addParameter("amount",Long.valueOf(withdraw.getAmount())*100);
//        builder.addParameter("receiverBankName",withdraw.getCardBank());
//        builder.addParameter("receiverBranchBankName",withdraw.getBackup2());//支行名称
//        builder.addParameter("receiverCardNo",withdraw.getCardNo());
//        builder.addParameter("receiverAccountName",withdraw.getCardPerson());
//        builder.addParameter("callbackURL", HOST + "/ddj/withDrawNotify/easyPayNotify");
//
//        ApiResponse response = apiClient.invoke(builder.build());
//        Map<String,String> map = response.getData(Map.class);
//        log.info(map.get("status"));
//        if ("PAY_FAILED".equals(map.get("status"))){
//            throw new BaseException(map.get("errorCode"));
//        }
//
//    }
}
